package com.bishe.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.api.EcoBootException;
import com.api.Result;
import com.api.entitis.Company;
import com.api.entitis.JobsInfo;
import com.api.entitis.model.LoginModel;
import com.api.entitis.model.UserModel;
import com.api.entitis.query.CompanyQuery;
import com.api.service.ICompanyService;
import com.api.service.IJobsInfoService;
import com.api.util.CompanyStpUtil;
import com.api.util.JobUtils;
import com.api.util.UserStpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin  //跨域处理
@RequestMapping("/company")
@Api(tags = "企业管理")
public class CompanyController {

    @Resource
    private ICompanyService companyService;

    @ApiOperation("企业登陆")
    @ApiOperationSupport(order = 1)
    @PostMapping("/Login")
    public Result companyLogin(@RequestBody LoginModel loginModel){
        //登录效验
        JobUtils.checkLoginModel(loginModel);

        UserModel login = companyService.login(loginModel);
        if (login!=null){
            return Result.ok("登陆成功!",login);
        }
        return Result.error("登陆失败!");
    }


    @ApiOperation("是否登录")
    @PostMapping("/isLogin")
    @SaCheckLogin(type = CompanyStpUtil.TYPE)
    public Result isLogin(){
        return Result.ok();
    }

    @ApiOperation("退出登录")
    @PostMapping("/loginOut")
    public Result loginOut( ){
        StpUtil.logout();
        CompanyStpUtil.logout();
        UserStpUtil.logout();

        return Result.ok();
    }


    @ApiOperation("企业注册")
    @PostMapping("/registerOrUpdate")
    public Result registerOrUpdate(@RequestBody Company company){
        if (company == null){
            return Result.error("注册信息不能为空！");
        }

        if (StrUtil.isEmpty(company.getCompanyName()) &&
                StrUtil.isEmpty(company.getCompanyEmail())&&
                StrUtil.isEmpty(company.getPassword())&&
                StrUtil.isEmpty(company.getType())){
            return Result.error("企业信息不能为空");
        }

        Boolean isEmail = JobUtils.isEmail(company.getCompanyEmail());
        if (!isEmail){
            return Result.error("邮箱格式不正确！");
        }
        //验证码校验
        JobUtils.checkImgCode(company.getTxt());
        companyService.registerOrUpdate(company);
        return Result.ok();
    }

    @ApiOperation("企业列表")
    @GetMapping("/list")
    public Result toPage(CompanyQuery query){
        IPage<Company> iPage = companyService.toPage(query);
        return Result.ok(iPage);
    }


    @ApiOperation("发布招聘列表")
    @GetMapping("/jobList")
    @SaCheckLogin(type = CompanyStpUtil.TYPE)
    public Result jobList(){
        List<JobsInfo> jobList = companyService.getJobList();
        return Result.ok(jobList);
    }

    @ApiOperation("删除招聘信息")
    @DeleteMapping("delete/{id}")
    public Result deleteById(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        companyService.removeById(id);
        return Result.ok();
    }

}
